const Cloud = require('../lib/av-core-min')
const userLogin = ()=>{
    // 触发用户授权
    wx.getUserProfile({
      desc: '请授权以便为您提供VIP服务',
      success:(res)=>{
        //   console.log(res); //弹窗授权获取头像、昵称
          let {avatarUrl,nickName} = res.userInfo
          Cloud.User.loginWithMiniApp().then(user => { //一键登录
            // console.log(user);
            let {id,_sessionToken} = user
            getApp().fly.config.headers["X-LC-Session"] = _sessionToken  //更新用户表必须携带验证信息
            getApp().fly.put(`/users/${id}`,{ //更新用户的头像、昵称
                avatar:avatarUrl,
                nickname:nickName
            }).then(res=>{
                let info = {
                    id,_sessionToken,
                    avatar:avatarUrl,
                    nickname:nickName
                }
                wx.setStorage({ //本地缓存用户信息
                    key:'userInfo',
                    data:info
                })
                getApp().globalData.userInfo = info //全局对象存储，方便提取
            })
          }).catch(console.error);
      },
      fail(err){
        wx.showToast({
            title: '很遗憾无法为您提供VIP服务',
            icon:'none'
        })
      }
    })
}

module.exports = {
    userLogin
}